package cn.telling.tianlianApple.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.telling.tianlianApple.dao.AreaFreightRulesMapper;
import cn.telling.tianlianApple.dao.AreaMapper;
import cn.telling.tianlianApple.dao.CarriersMapper;
import cn.telling.tianlianApple.dao.ShopExpressMapper;
import cn.telling.tianlianApple.dao.ShopFreightRulesMapper;
import cn.telling.tianlianApple.dao.SupplyMapper;
import cn.telling.tianlianApple.dao.TmAppleShopMapper;
import cn.telling.tianlianApple.service.IAppleRulesService;
import cn.telling.tianlianApple.vo.AreaFreightRulesVo;
import cn.telling.tianlianApple.vo.AreaInfoVo;
import cn.telling.tianlianApple.vo.CarriersVo;
import cn.telling.tianlianApple.vo.ShopFreightRulesVo;
import cn.telling.tianlianApple.vo.SupplyVo;
import cn.telling.tianlianApple.vo.TmAppleShop;
import cn.telling.utils.StringHelperTools;

@Service("appleRulesService")
public class AppleRulesServiceImpl implements IAppleRulesService{

	@Autowired
	private TmAppleShopMapper appleShopMapper;
	@Autowired
	private ShopFreightRulesMapper appleShopFreightRulesMapper;
	@Autowired
	private AreaFreightRulesMapper appleAreaFreightRulesMapper;
	@Autowired
	private SupplyMapper appleSupplyMapper;
	@Autowired
	private AreaMapper appleAreaMapper;
	@Autowired
	private CarriersMapper appleCarriersMapper;
	@Autowired
	private ShopExpressMapper appleShopExpressMapper;
	
	@Override
	public TmAppleShop getShopInfoByUserid(BigDecimal userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		return appleShopMapper.selectShopInfo(params);
	}

	@Override
	public List<ShopFreightRulesVo> getShopFreightRulesByShopId(String shopId) {
		
		//装载返回参数
		List<ShopFreightRulesVo> resultlist = new ArrayList<ShopFreightRulesVo>();
		//参数设置
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("shopId", shopId);
		//params.put("rulesType", ruleType);
		
		List<ShopFreightRulesVo> nowlist = appleShopFreightRulesMapper.selectShopExpressCarrier(params);
		
		return  this.getShopRulesDetils(nowlist, resultlist,params);
	
	}
	
	
	
	public List<ShopFreightRulesVo> getShopRulesDetils(List<ShopFreightRulesVo> nowlist,List<ShopFreightRulesVo> resultlist,Map<String, Object> params){
		if (nowlist != null && nowlist.size() > 0) {
			if (!"1".equals(nowlist.get(0).getType())) {
				ShopFreightRulesVo vo = new ShopFreightRulesVo();
				vo.setType("1");
				vo.setCheckflag("f");
				nowlist.add(0, vo);
			}

			if ((nowlist.size() > 1 && !"2".equals(nowlist.get(1).getType())) || nowlist
					.size() == 1) {
				ShopFreightRulesVo vo1 = new ShopFreightRulesVo();
				vo1.setType("2");
				vo1.setCheckflag("f");
				nowlist.add(1, vo1);
			}

			if ((nowlist.size() > 2 && !"3".equals(nowlist.get(2).getType())) || nowlist
					.size() == 2) {
				ShopFreightRulesVo vo2 = new ShopFreightRulesVo();
				vo2.setType("3");
				vo2.setCheckflag("f");
				nowlist.add(2, vo2);
			}
			if (!"5".equals(nowlist.get(nowlist.size()-1).getType())) {
				ShopFreightRulesVo vo3 = new ShopFreightRulesVo();
				vo3.setType("5");
				vo3.setCheckflag("f");
				nowlist.add(vo3);
			}
			resultlist = nowlist;
		} else {
			ShopFreightRulesVo vo = new ShopFreightRulesVo();
			vo.setType("1");
			vo.setCheckflag("f");
			resultlist.add(vo);
			ShopFreightRulesVo vo1 = new ShopFreightRulesVo();
			vo1.setType("2");
			vo1.setCheckflag("f");
			resultlist.add(vo1);
			ShopFreightRulesVo vo2 = new ShopFreightRulesVo();
			vo2.setType("3");
			vo2.setCheckflag("f");
			resultlist.add(vo2);
			ShopFreightRulesVo vo3 = new ShopFreightRulesVo();
			vo3.setType("5");
			vo3.setCheckflag("f");
			resultlist.add(vo3);
		}

		for(int i=0;i<resultlist.size();i++){
			String freightid = StringHelperTools.nvl(resultlist.get(i).getId());
			params.put("freightid", freightid);
			if(!"".equals(freightid)){
				List<AreaFreightRulesVo> arealist = appleAreaFreightRulesMapper.selectAreaFreightRulesByFreightid(params);
				if(arealist!=null && arealist.size()>0){
					resultlist.get(i).setAreaFreightList(arealist);
				}
			}
		}
		return resultlist;
	}

	@Override
	public SupplyVo findSupplyByUserId(String userId) {
		 return appleSupplyMapper.findSupplyByUserId(userId);
	}

	@Override
	public List<AreaInfoVo> getSaleAreaInfoBySupplyId(String supplyId) {

       // SupplyVo supply = appleSupplyMapper.findSupplyBySupplyId(supplyId);
        List<AreaInfoVo> saleAreaInfoList = null;
       // String isControlSale = StringHelperTools.nvl(supply.getIsControlSale());
   /*     if(isControlSale.equals(TianlianConstance.IS_CONTROL_SALE_1)) {
            // 取指定的区域
            saleAreaInfoList = appleAreaMapper.findSaleAreaInfoListBySupplyId(supplyId);
        } else {*/
            // 获取中国
            List<AreaInfoVo> chinaAreaInfo = appleAreaMapper.findAreaInfoListByParentIds(new String[]{"0"});
            // 取全国省级区域
            saleAreaInfoList = appleAreaMapper.findAreaInfoListByParentIds(new String[]{chinaAreaInfo.get(0).getAreaId()});
       // }
        return saleAreaInfoList;
    
	}

	@Override
	public List<CarriersVo> getShopExpressList(String shopId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("shopId", shopId);
		return appleCarriersMapper.selectShopExpressCarrier(params);
	}

	@Override
	public void insertNewShopFreightRule(TmAppleShop appleShopInfo,
			List<CarriersVo> sels) {
		//删除存在的承运商
		appleShopExpressMapper.deleteShopExpress(appleShopInfo);//CarriersVo
		//店铺承运商插入数据
		for (int i = 0; i < sels.size(); i++) {
			CarriersVo vos = sels.get(i);
			vos.setShopExpressId(UUID.randomUUID().toString().replaceAll("-", "").trim());
		}
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("sels", sels);
		
		appleShopExpressMapper.insertShopExpress(params);
	
	}

	@Override
	public void insertNewShopFreightRules(TmAppleShop appleShopInfo,
			List<ShopFreightRulesVo> ls) {

		// 删除存在的运费规则
		appleShopFreightRulesMapper.deleteShopFreightRules(appleShopInfo);//删除已有的运费规则
		appleAreaFreightRulesMapper.deleteAreaFreightRules(appleShopInfo);//删除已有的运费规则
		List<AreaFreightRulesVo> areals = new ArrayList<AreaFreightRulesVo>();
		for(int i=0;i<ls.size();i++){
			String id = UUID.randomUUID().toString().replaceAll("-", "");
			ls.get(i).setId(id);
			String[] a = ls.get(i).getAreaids().split(",");
			ls.get(i).setSort(new BigDecimal(i));
			for(int j=0;j<a.length;j++){
				AreaFreightRulesVo asvo = new AreaFreightRulesVo();
				asvo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
				asvo.setAreaid(new BigDecimal(a[j]));
				asvo.setFreightid(id);
				asvo.setShopId(appleShopInfo.getId());
				areals.add(asvo);
			}
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
		//店铺区域运费规则插入数据
		params.put("areals", areals);
		appleAreaFreightRulesMapper.insertAreaFreightRules(params);
		//店铺运费规则插入数据
		params.put("ls", ls);
		appleShopFreightRulesMapper.insertNewShopFreightRules(params);
	}

	@Override
	public List<ShopFreightRulesVo> getShopFreightRulesByShopId(
			BigDecimal supplyArea0Id) {
		
		
		//装载返回参数
		List<ShopFreightRulesVo> resultlist = new ArrayList<ShopFreightRulesVo>();
		//参数设置
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("supplyArea0Id", supplyArea0Id);
		ShopFreightRulesVo sfrVo = appleShopFreightRulesMapper.findShopIdAndSortIdBySupplyArea0Id(params);
		params.put("shopId", sfrVo.getShopId());
		
		
		List<ShopFreightRulesVo> nowlist = appleShopFreightRulesMapper.selectShopExpressCarrier(params);
		
		return  this.getShopRulesDetils(nowlist, resultlist,params);
	}

	@Override
	public ShopFreightRulesVo getShopFreightRulesByShopId(String shopId,
			String areaId) {
		
		//参数设置
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("shopId", shopId);
		params.put("areaId", areaId);
		
		ShopFreightRulesVo shopFreightRules = appleShopFreightRulesMapper.findShopFreightRulesByShopIdAndAreaId(params);
		
		return  shopFreightRules==null?new ShopFreightRulesVo():shopFreightRules;
	}
	
	
	
	@Override
	public ShopFreightRulesVo getShopFreightRulesById(String supplyArea0Id,
			String areaId) {
		
		
		//参数设置
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("supplyArea0Id", supplyArea0Id);
		TmAppleShop shopInfo = appleShopMapper.selectShopInfoBySupplyArea0Id(params);
		params.put("shopId", shopInfo.getId());
		params.put("areaId", areaId);
		
		ShopFreightRulesVo shopFreightRules = appleShopFreightRulesMapper.findShopFreightRulesByShopIdAndAreaId(params);
		
		return  shopFreightRules==null?new ShopFreightRulesVo():shopFreightRules;
	}

	@Override
	public List<CarriersVo> getShopExpressListByUserId(String userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		return appleCarriersMapper.selectShopExpressListByUserId(params);
	}

}
